package cn.wangqi.personal.controller;

import cn.wangqi.personal.common.BaseController;
import cn.wangqi.personal.domain.ImageMeta;
import cn.wangqi.personal.upload.MultipartResolverFileUtil;
import cn.wangqi.personal.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by admin on 2017/9/9.
 */
@Controller
public class UeditorController extends BaseController {
    Logger logger = Logger.getLogger(BaseController.class);

    @RequestMapping("/ueditor.html")
    @ResponseBody
    public String ueditorIndex(String action,HttpServletRequest request){
        if("config".equals(action)){
            return "{\"imageActionName\":\"uploadimage\",\"imageFieldName\":\"upfile\",\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imageCompressEnable\":true,\"imageCompressBorder\":1600,\"imageInsertAlign\":\"center\",\"imageUrlPrefix\":\"\",\"imagePathFormat\":\"\\/server\\/ueditor\\/upload\\/image\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"scrawlActionName\":\"uploadscrawl\",\"scrawlFieldName\":\"upfile\",\"scrawlPathFormat\":\"\\/server\\/ueditor\\/upload\\/image\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"scrawlMaxSize\":2048000,\"scrawlUrlPrefix\":\"\",\"scrawlInsertAlign\":\"none\",\"snapscreenActionName\":\"uploadimage\",\"snapscreenPathFormat\":\"\\/server\\/ueditor\\/upload\\/image\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"snapscreenUrlPrefix\":\"\",\"snapscreenInsertAlign\":\"none\",\"catcherLocalDomain\":[\"127.0.0.1\",\"localhost\",\"img.baidu.com\"],\"catcherActionName\":\"catchimage\",\"catcherFieldName\":\"source\",\"catcherPathFormat\":\"\\/server\\/ueditor\\/upload\\/image\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"catcherUrlPrefix\":\"\",\"catcherMaxSize\":2048000,\"catcherAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"videoActionName\":\"uploadvideo\",\"videoFieldName\":\"upfile\",\"videoPathFormat\":\"\\/server\\/ueditor\\/upload\\/video\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"videoUrlPrefix\":\"\",\"videoMaxSize\":102400000,\"videoAllowFiles\":[\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\"],\"fileActionName\":\"uploadfile\",\"fileFieldName\":\"upfile\",\"filePathFormat\":\"\\/server\\/ueditor\\/upload\\/file\\/{yyyy}{mm}{dd}\\/{time}{rand:6}\",\"fileUrlPrefix\":\"\",\"fileMaxSize\":51200000,\"fileAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"],\"imageManagerActionName\":\"listimage\",\"imageManagerListPath\":\"\\/server\\/ueditor\\/upload\\/image\\/\",\"imageManagerListSize\":20,\"imageManagerUrlPrefix\":\"\",\"imageManagerInsertAlign\":\"none\",\"imageManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"fileManagerActionName\":\"listfile\",\"fileManagerListPath\":\"\\/server\\/ueditor\\/upload\\/file\\/\",\"fileManagerUrlPrefix\":\"\",\"fileManagerListSize\":20,\"fileManagerAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\",\".flv\",\".swf\",\".mkv\",\".avi\",\".rm\",\".rmvb\",\".mpeg\",\".mpg\",\".ogg\",\".ogv\",\".mov\",\".wmv\",\".mp4\",\".webm\",\".mp3\",\".wav\",\".mid\",\".rar\",\".zip\",\".tar\",\".gz\",\".7z\",\".bz2\",\".cab\",\".iso\",\".doc\",\".docx\",\".xls\",\".xlsx\",\".ppt\",\".pptx\",\".pdf\",\".txt\",\".md\",\".xml\"]}";
        }else if("listimage".equals(action)){
            String patform = request.getParameter("scene");
            String start = request.getParameter("start");
            String size = request.getParameter("size");
            if(StringUtil.isEmpty(patform)){
                patform = "paths/";
            }
            if(StringUtil.isEmpty(start)){
                return "start is null";
            }
            if(StringUtil.isEmpty(size)){
                return "size is null";
            }
            try {
                ImageMeta imageMeta = new ImageMeta();
                imageMeta.setState(ImageMeta.SUCCESS);
                String result = StringUtil.JSON_MAPPER_NOTNULL.writeValueAsString(imageMeta);
                return result;
            }catch (Exception e){
                logger.error(e.getMessage(),e);
                return "{'state':'"+ImageMeta.FAIL+"','list':[],'start':'"+start+"','total':0}";
            }
        }else if("uploadimage".equals(action)){
            return MultipartResolverFileUtil.uploadFile(request);
        }
        return "";
    }
}
